/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.model.queue;

import java.util.Vector;
import lib.plan.Item;

/**
 * This abstract class represents a item queue but doesn't define how the items
 * are added. To poll or peek items, always returns the value in the head of
 * the queue but how to add items depends on the implementation of the queue.
 * @see Item
 * @author Jorge Mira Yagüe
 */
public abstract class Queue {

    private QueueType type;
    protected int size;
    protected Vector<Item> elements;

    /**
     * Builds a queue
     * @param type the type of the queue
     * @see QueueType
     */
    public Queue(QueueType type) {
        this.type = type;
        elements = new Vector<Item>();
    }

    public QueueType getType() {
        return type;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public abstract void add(Item item);

    /**
     * Returns but doesn't remove the item in the head of the queue
     * @return the item in the head of the queue, or null if the queue is empty
     */
    public Item peek() {
        if (isEmpty()) {
            return null;
        } else {
            return elements.firstElement();
        }
    }

    /**
     * Returns and removes the item in the head of the queue
     * @return the item in the head of the queue, or null if the queue is empty
     */
    public Item poll() {
        if (isEmpty()) {
            return null;
        } else {
            size--;
            return elements.remove(0);
        }
    }

    public Vector<Item> getElements() {
        return elements;
    }

    public int size() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
